home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / file.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  17KB  |  498 lines

  1. /*
  2.  * The routines in this file
  3.  * handle the reading and writing of
  4.  * disk files. All of details about the
  5.  * reading and writing of the disk are
  6.  * in "fileio.c".
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. unsigned char   insflag = FALSE;        /* Flag for C mode in newline() */
  12.  
  13. /*
  14.  * Read a file into the current
  15.  * buffer. This is really easy; all you do it
  16.  * find the name of the file, and call the standard
  17.  * "read a file into the current buffer" code.
  18.  * Bound to "C-X C-R".
  19.  */
  20. fileread(f, n)
  21. register int f, n;
  22. {
  23.         register int    s;
  24.         char            fname[NFILEN];
  25.  
  26.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  27.                 return (s);
  28.         return (readin(fname));
  29. }
  30.  
  31. /*
  32.  * Select a file for editing.
  33.  * Look around to see if you can find the
  34.  * file in another buffer; if you can find it
  35.  * just switch to the buffer. If you cannot find
  36.  * the file, create a new buffer, read in the
  37.  * text, and switch to the new buffer.
  38.  * Bound to C-X C-V.
  39.  */
  40. filevisit(f, n)
  41. register int f, n;
  42. {
  43.         register BUFFER *bp;
  44.         register WINDOW *wp;
  45.         register LINE   *lp;
  46.         register int    i;
  47.         register int    s;
  48.         char            bname[NBUFN];
  49.         char            fname[NFILEN];
  50.  
  51.         if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  52.                 return (s);
  53.         strcpy(lastbuf, curbp->b_bname);
  54.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  55.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  56.                         if (--curbp->b_nwnd == 0) {
  57.                                 curbp->b_dotp  = curwp->w_dotp;
  58.                                 curbp->b_doto  = curwp->w_doto;
  59.                                 curbp->b_markp = curwp->w_markp;
  60.                                 curbp->b_marko = curwp->w_marko;
  61.                         }
  62.                         curbp = bp;
  63.                         curwp->w_bufp  = bp;
  64.                         if (bp->b_nwnd++ == 0) {
  65.                                 curwp->w_dotp  = bp->b_dotp;
  66.                                 curwp->w_doto  = bp->b_doto;
  67.                                 curwp->w_markp = bp->b_markp;
  68.                                 curwp->w_marko = bp->b_marko;
  69.                         } else {
  70.                                 wp = wheadp;
  71.                                 while (wp != NULL) {
  72.                                         if (wp!=curwp && wp->w_bufp==bp) {
  73.                                                 curwp->w_dotp  = wp->w_dotp;
  74.                                                 curwp->w_doto  = wp->w_doto;
  75.                                                 curwp->w_markp = wp->w_markp;
  76.                                                 curwp->w_marko = wp->w_marko;
  77.                                                 break;
  78.                                         }
  79.                                         wp = wp->w_wndp;
  80.                                 }
  81.                         }
  82.                         lp = curwp->w_dotp;
  83.                         i = curwp->w_ntrows/2;
  84.                         while (i-- && lback(lp)!=curbp->b_linep)
  85.                                 lp = lback(lp);
  86.                         curwp->w_linep = lp;
  87.                         curwp->w_flag |= WFMODE|WFHARD;
  88.                         mlwrite("[Old buffer]");
  89.                         return (TRUE);
  90.                 }
  91.         }
  92.         makename(bname, fname);                 /* New buffer name.     */
  93.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  94.                 s = mlreply("Buffer name: ", bname, NBUFN);
  95.                 if (s == ABORT)                 /* ^G to just quit      */
  96.                         return (s);
  97.                 if (s == FALSE) {               /* CR to clobber it     */
  98.                         makename(bname, fname);
  99.                         break;
  100.                 }
  101.         }
  102.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  103.                 mlwrite("Cannot create buffer");
  104.                 return (FALSE);
  105.         }
  106.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  107.                 curbp->b_dotp = curwp->w_dotp;
  108.                 curbp->b_doto = curwp->w_doto;
  109.                 curbp->b_markp = curwp->w_markp;
  110.                 curbp->b_marko = curwp->w_marko;
  111.         }
  112.         curbp = bp;                             /* Switch to it.        */
  113.         curwp->w_bufp = bp;
  114.         curbp->b_nwnd++;
  115.         return (readin(fname));                 /* Read it in.          */
  116. }
  117.  
  118. /*
  119.  * Read file "fname" into the current
  120.  * buffer, blowing away any text found there. Called
  121.  * by both the read and visit commands. Return the final
  122.  * status of the read. Also called by the mainline,
  123.  * to read in a file specified on the command line as
  124.  * an argument.
  125.  */
  126. readin(fname)
  127. char    fname[];
  128. {
  129.         register LINE   *lp1;
  130.         register LINE   *lp2;
  131.         register int    i;
  132.         register WINDOW *wp;
  133.         register BUFFER *bp;
  134.         register int    s;
  135.         register int    nbytes;
  136.         register int    nline;
  137.         char            line[NLINE];
  138.  
  139.         bp = curbp;                             /* Cheap.               */
  140.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  141.                 return (s);
  142.         bp->b_flag &= ~(BFTEMP|BFCHG);
  143.         if (fname[0] == '~')                    /* an alias request */
  144.                 if ((s=parsefn(fname))==FALSE)
  145.                         s = FIOFNF;
  146.         strcpy(bp->b_fname, fname);
  147.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  148.                 goto out;
  149.         if (s == FIOFNF) {                      /* File not found.      */
  150.                 mlwrite("[New file]");
  151.                 goto out;
  152.         }
  153.         mlwrite("[Reading %s]",fname);
  154.         nline = 0;
  155.         while ((s=ffgetline(line, NLINE)) == FIOSUC) {
  156.                 nbytes = strlen(line);
  157.                 if ((lp1=lalloc(nbytes)) == NULL) {
  158.                         s = FIOERR;             /* Keep message on the  */
  159.                         break;                  /* display.             */
  160.                 }
  161.                 lp2 = lback(curbp->b_linep);
  162.                 lp2->l_fp = lp1;
  163.                 lp1->l_fp = curbp->b_linep;
  164.                 lp1->l_bp = lp2;
  165.                 curbp->b_linep->l_bp = lp1;
  166.                 for (i=0; i<nbytes; ++i)
  167.                         lputc(lp1, i, line[i]);
  168.                 ++nline;
  169.         }
  170.         ffclose();                              /* Ignore errors.       */
  171.         if (s == FIOEOF) {                      /* Don't zap message!   */
  172.                 if (nline == 1)
  173.                         mlwrite("[Read %d line]",nline);
  174.                 else
  175.                         mlwrite("[Read %d lines]", nline);
  176.         }
  177. out:
  178.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  179.                 if (wp->w_bufp == curbp) {
  180.                         wp->w_linep = lforw(curbp->b_linep);
  181.                         wp->w_dotp  = lforw(curbp->b_linep);
  182.                         wp->w_doto  = 0;
  183.                         wp->w_markp = curwp->w_dotp;
  184.                         wp->w_marko = 0;
  185.                         wp->w_flag |= WFMODE|WFHARD;
  186.                 }
  187.         }
  188.         if (s == FIOERR)                        /* False if error.      */
  189.                 return (FALSE);
  190.         return (TRUE);
  191. }
  192.  
  193. /*
  194.  * Take a file name, and from it
  195.  * fabricate a buffer name. This routine knows
  196.  * about the syntax of file names on the target system.
  197.  * I suppose that this information could be put in
  198.  * a better place than a line of code.
  199.  */
  200. makename(bname, fname)
  201. char    bname[];
  202. char    fname[];
  203. {
  204.         register char   *cp1;
  205.         register char   *cp2;
  206.  
  207.         if (fname[0] == '~')
  208.                 parsefn(fname);
  209.         cp1 = &fname[0];
  210.         while (*cp1 != 0)
  211.                 ++cp1;
  212. #if     VMS
  213.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  214.                 --cp1;
  215. #endif
  216. #if     CPM
  217.         while (cp1!=&fname[0] && cp1[-1]!=':')
  218.                 --cp1;
  219. #endif
  220. #if     ST
  221.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  222.                 --cp1;
  223. #endif
  224. #if     V7
  225.         while (cp1!=&fname[0] && cp1[-1]